﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace xf.Service
{
    public class DbService
    {
        private static SqlSugarClient sqlSugarClient = null;

        /// <summary>
        /// 获取SqlSugarClient链接
        /// </summary>
        /// <returns></returns>
        public static SqlSugarClient GetSqlSugarClient() {
            if (sqlSugarClient == null)
            {
                sqlSugarClient = new SqlSugarClient(
                new ConnectionConfig()
                {
                    //ConnectionString = "server=.;uid=sa;pwd=123456;database=xf",
                    //ConnectionString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString,
                    //DbType = DbType.SqlServer,//设置数据库类型
                    //ConnectionString = @"DataSource=F:\xf\xf\db\xf.db",
                    ConnectionString = ConfigurationManager.ConnectionStrings["connStrSqlite"].ConnectionString,
                    DbType = DbType.Sqlite,//设置数据库类型
                    IsAutoCloseConnection = true,//自动释放数据务，如果存在事务，在事务结束后释放
                    InitKeyType = InitKeyType.Attribute //从实体特性中读取主键自增列信息
                });

            }

            //用来打印Sql方便你调式    
            sqlSugarClient.Aop.OnLogExecuting = (sql, pars) =>
            {
                Console.WriteLine(sql + "\r\n" +
                sqlSugarClient.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                Console.WriteLine();
            };
            return sqlSugarClient;
        }
    }
}
